﻿/***************************
* 1.周边列表
****************************/
 
/* 变量 */
var nearby = "experience/nearby.html";

/***********    地图使用公共方法    **********/
//地图跳转-类似周边，跳转景区，要iframe能使用
function MapClickJump(url, param) {
    mainView.router.loadPage({ url: url, reload: false });
}
//调用语音接口，返回给子界面--地图使用
var voice = {
    localId: '',
    serverId: ''
};
function ReturnStartVoice() {
    wx.startRecord({
        cancel: function () {
            alert('用户拒绝授权录音');
        }
    });
};
function ReturnEndVoid(callback) {
    wx.stopRecord({
        success: function (res) {
            voice.localId = res.localId;
            wx.translateVoice({
                localId: voice.localId, // 需要识别的音频的本地Id，由录音相关接口获得
                isShowProgressTips: 1, // 默认为1，显示进度提示
                success: function (res) {
                    if (res.translateResult != 'undefined' && res.translateResult != '') {
                        yy = res.translateResult.replace(/['",.!?，。？！]/ig, ""); // 语音识别的结果
                        callback(yy);
                    }
                }
            });
        },
        fail: function (res) {
            alert(JSON.stringify(res));
        }
    });
};
////语音按钮事件
///* 初始化语音config */
//var gn = ['startRecord', 'stopRecord', 'translateVoice', 'openLocation', 'getLocation'];
//wx_config(gn, function () {
//    //callback 所有JSSDK事件
//}, function () {
//    //errorcallback  config注册失败事件
//});
/***********    地图使用公共方法----结束    **********/

/* 周边 */
var contactsCallback_nearby = myApp.onPageInit('nearby', function (page) {
    var longitude = "";
    var latitude = "";
    var yy = "";
    var gn = ['startRecord', 'stopRecord', 'translateVoice', 'openLocation', 'getLocation'];
    // wx_config(gn, function () {
    //     wx.getLocation({
    //         type: 'gcj02', // 默认为wgs84的gps坐标，如果要返回直接给openLocation用的火星坐标，可传入'gcj02'
    //         success: function (res) {

    //             ISS_Constants.User.latitude = res.latitude; // 纬度，浮点数，范围为90 ~ -90
    //             ISS_Constants.User.longitude = res.longitude; // 经度，浮点数，范围为180 ~ -180。
    //             // 速度，以米/每秒计res.speed;
    //             // 位置精度res.accuracy;
    //             if (longitude == undefined || longitude == "" || latitude == undefined || latitude == "") {
    //                 longitude = ISS_Constants.User.longitude;
    //                 latitude = ISS_Constants.User.latitude;
    //             }
    //             LoadData(index_tab1, size);
    //         }
    //     });
    // }, function () {
    //     LoadData(index_tab1, size);
    // });
    // 3 智能接口
    var voice = {
        localId: '',
        serverId: ''
    };
    $$('.popup-about').on('opened', function () {
        myApp.popup('.popup-about');
        wx.startRecord({
            cancel: function () {
                alert('用户拒绝授权录音');
            }
        });
    });

    $$('.close-popup').on('click', function () {
        myApp.popup('.popup-services');
        wx.stopRecord({
            success: function (res) {
                voice.localId = res.localId;
                wx.translateVoice({
                    localId: voice.localId, // 需要识别的音频的本地Id，由录音相关接口获得
                    isShowProgressTips: 1, // 默认为1，显示进度提示
                    success: function (res) {
                        if (res.translateResult != 'undefined' && res.translateResult != '') {
                            yy = res.translateResult.replace(/['",.!?，。？！]/ig, ""); // 语音识别的结果
                            if (current_tabindex == 0) {
                                index_tab1 = 1;
                                LoadData(index_tab1, size)
                            } else if (current_tabindex == 1) {
                                index_tab2 = 1;
                                LoadData(index_tab2, size)
                            } else if (current_tabindex == 2) {
                                index_tab3 = 1;
                                LoadData(index_tab3, size)
                            } else if (current_tabindex == 3) {
                                index_tab4 = 1;
                                LoadData(index_tab4, size)
                            } else if (current_tabindex == 4) {
                                index_tab5 = 1;
                                LoadData(index_tab5, size)
                            }
                        }
                    }
                });
            },
            fail: function (res) {
                alert(JSON.stringify(res));
            }
        });
    });

    //删除滚动后重新添加滚动
    var flag_InfiniteScroll = true;
    //是否需要滚动加载
    var flag_needScroll = true;
    //滚动事件只执行一次
    var flag_execute = false;

    var index_tab1 = 1;
    var index_tab2 = 1;
    var index_tab3 = 1;
    var index_tab4 = 1;
    var index_tab5 = 1;
    var current_tabindex = 0;
    var size = 10;

    // 默认加载 第一个
    LoadData(index_tab1, size);
    // 加载数据
    function LoadData(pageindex, pagesize) {
        //如果是第一页，清除原有数据
        if (current_tabindex == 0 && index_tab1 == 1) {
            $$("#sub_nearby_tab1 ul").html("");
        } else if (current_tabindex == 1 && index_tab2 == 1) {
            $$("#sub_nearby_tab2 ul").html("");
        } else if (current_tabindex == 2 && index_tab3 == 1) {
            $$("#sub_nearby_tab3 ul").html("");
        } else if (current_tabindex == 3 && index_tab4 == 1) {
            $$("#sub_nearby_tab4 ul").html("");
        } else if (current_tabindex == 4 && index_tab5 == 1) {
            $$("#sub_nearby_tab5 ul").html("");
        }
         myApp.showPreloader();
        if (current_tabindex == 0) { // 开始景区
            var v_order = [];
            var v_search = [];
            if (yy != "") {
                v_search.push({
                    SearchField: "Keyword",
                    ConditionType: 2,
                    AndOR: "and",
                    ListSearchValue: [{
                        ItemValue: yy
                    }]
                });
            }
            var opt = {
                OrderField: "Distance",
                OrderType: "asc"
            };
            v_order.push(opt);
            // 参数
            var _content = JSON.stringify({
                type: 0,
                conditionParams: {
                    Page: {
                        PageSize: pagesize,
                        PageIndex: pageindex
                    },
                    Search: v_search,
                    Order: v_order,
                },
                lng: iss_localStorage.get("yuqing_longitude"),
                lat: iss_localStorage.get("yuqing_latitude"),
            });
            

            $$.ajax({
                url: ISS_Constants.Url.defaulturl + ISS_Constants.serviceName.ATTAR,
                data: { content: _content },
                
                dataType: "jsonp",
                method: "POST",
                crossDomain: true,
                success: function (data, status) {
                     myApp.hidePreloader();
                    data = JSON.parse(data);
                    loading=false;
                    if (data && data.status == "0" && data.results.length > 0) {
                        // 格式化数据
                        for (var i = 0; i < data.results.length; i++) {
                            if (data.results[i].getImage && data.results[i].getImage.smallImageFrontUrl && (
                                            data.results[i].getImage.smallImageFrontUrl == null || data.results[i].getImage.smallImageFrontUrl == "")) {
                                data.results[i].getImage.smallImageFrontUrl = NoPicture();
                            };
                            data.results[i].distance = ISS.utils.toFixed(data.results[i].distance, 2);
                        }
                        var searchTemplate = $$('#template-nearby-tab1').html();
                        if (searchTemplate) {
                            var compiledTemplate = Template7.compile(searchTemplate);
                            var html = compiledTemplate(data);
                            $$("#sub_nearby_tab1 ul").append(html);
                            flag_InfiniteScroll = flag_needScroll = true;
                        }
                        if ($$("sub_nearby_tab1 .content-block ul li").length >= data.total) {
                            // 删除加载提示符
                            $$('.infinite-scroll-preloader1').remove();
                            // 加载完毕，则注销无限加载事件，以防不必要的加载
                            myApp.detachInfiniteScroll($$('.infinite-scroll'));
                        }
                    }
                    else {
                        // 删除加载提示符
                        myApp.detachInfiniteScroll($$('.infinite-scroll'));
                        if (current_tabindex == 0) {
                            $$('.infinite-scroll-preloader1').remove();
                            index_tab1--;
                        }
                    }
                    yy = "";
                },
                error: function (xhr, status) {
                     myApp.hidePreloader();
                    console.log("ajax error!");
                    loading=false;
                    yy = "";
                }
            });

        } //end if() 景区
        else if (current_tabindex == 1) { // 酒店开始
            var v_order = [];
            var v_search = [];
            var re = new RegExp("-", "g");
            var v_start = GetDate(new Date()).replace(re, ".");   //入住时间
            var v_end = GetDate(Date.prototype.DateAdd(new Date(), 'd', 1)).replace(re, ".");    //离店时间
            if (yy != "") {
                v_search.push({
                    SearchField: "HotelKey",
                    ConditionType: 2,
                    AndOR: "and",
                    ListSearchValue: [{
                        ItemValue: yy
                    }]
                });
            }
            var opt = {
                OrderField: "Distance",
                OrderType: "asc"
            };
            v_order.push(opt);
            var _content = JSON.stringify({
                type: 0,
                conditionParams: {
                    Page: {
                        PageSize: pagesize,
                        PageIndex: pageindex
                    },
                    Search: v_search,
                    Order: v_order,
                },
                lng: iss_localStorage.get("yuqing_longitude"),
                lat: iss_localStorage.get("yuqing_latitude"),
            });

            
            $$.ajax({
                url: ISS_Constants.Url.defaulturl + ISS_Constants.serviceName.HOTEL,
                data: { content: _content },
                
                dataType: "jsonp",
                method: "POST",
                success: function (data, status) {
                     myApp.hidePreloader();
                    data = JSON.parse(data);
                    loading=false;
                    if (data && data.status == "0" && data.results.length > 0) {
                        // 格式化数据
                        for (var i = 0; i < data.results.length; i++) {
                            if (data.results[i].getImage && data.results[i].getImage.smallImageFrontUrl && (
                                data.results[i].getImage.smallImageFrontUrl == null || data.results[i].getImage.smallImageFrontUrl == "")) {
                                data.results[i].getImage.smallImageFrontUrl = NoPicture();
                            };
                            data.results[i].distance = ISS.utils.toFixed(data.results[i].distance, 2);
                        }
                        var searchTemplate = $$('#template-nearby-tab2').html();
                        if (searchTemplate) {
                            var compiledTemplate = Template7.compile(searchTemplate);
                            var html = compiledTemplate(data);
                            $$("#sub_nearby_tab2 ul").append(html);
                            flag_InfiniteScroll = flag_needScroll = true;
                        }
                        if ($$("sub_nearby_tab2 .content-block ul li").length >= data.total) {
                            // 删除加载提示符
                            $$('.infinite-scroll-preloader2').remove();
                            // 加载完毕，则注销无限加载事件，以防不必要的加载
                            myApp.detachInfiniteScroll($$('.infinite-scroll'));
                        }
                    }
                    else {
                        // 删除加载提示符
                        myApp.detachInfiniteScroll($$('.infinite-scroll'));
                        if (current_tabindex == 1) {
                            $$('.infinite-scroll-preloader2').remove();
                            index_tab2--;
                        }
                    }
                    yy = "";
                },
                error: function (xhr, status) {
                     myApp.hidePreloader();
                    console.log("ajax error!");
                    loading=false;
                    yy = "";
                }
            });
        } //end if() 酒店
        else if (current_tabindex == 2) {  // 餐厅开始
            var v_order = [];
            var v_search = [];
            var opt = {
                OrderField: "Distance",
                OrderType: "asc"
            };
            v_order.push(opt);
            if (yy != "") {
                v_search.push({
                    SearchField: "CnName",
                    ConditionType: 2,
                    AndOR: "and",
                    ListSearchValue: [{
                        ItemValue: yy
                    }]
                });
            }
            var _content = JSON.stringify({
                type: 0,
                conditionParams: {
                    Page: {
                        PageSize: pagesize,
                        PageIndex: pageindex
                    },
                    Search: v_search,
                    Order: v_order,
                },
                lng: iss_localStorage.get("yuqing_longitude"),
                lat: iss_localStorage.get("yuqing_latitude"),
            });

            $$.ajax({
                url: ISS_Constants.Url.defaulturl + ISS_Constants.serviceName.RESTAURANT,
                data: { content: _content },
                crossDomain: true,
                dataType: "jsonp",
                method: "POST",
                success: function (data, status) {
                     myApp.hidePreloader();
                    data = JSON.parse(data);
                    loading=false;
                    if (data && data.status == "0" && data.results.length > 0) {
                        // 格式化数据
                        for (var i = 0; i < data.results.length; i++) {
                            if (data.results[i].GetImage && data.results[i].GetImage.SmallImageFrontUrl && (
                                data.results[i].GetImage.SmallImageFrontUrl == null || data.results[i].GetImage.SmallImageFrontUrl == "")) {
                                data.results[i].GetImage.SmallImageFrontUrl = NoPicture();
                            };
                            data.results[i].distance = ISS.utils.toFixed(data.results[i].distance, 2);
                        }
                        var searchTemplate = $$('#template-nearby-tab3').html();
                        if (searchTemplate) {
                            var compiledTemplate = Template7.compile(searchTemplate);
                            var html = compiledTemplate(data);
                            $$("#sub_nearby_tab3 ul").append(html);
                            flag_InfiniteScroll = flag_needScroll = true;
                        }
                        if ($$("#sub_nearby_tab3 .content-block ul li").length >= data.total) {
                            // 删除加载提示符
                            $$('.infinite-scroll-preloader3').remove();
                            // 加载完毕，则注销无限加载事件，以防不必要的加载
                            myApp.detachInfiniteScroll($$('.infinite-scroll'));
                        }
                    }
                    else {
                        // 删除加载提示符
                        myApp.detachInfiniteScroll($$('.infinite-scroll'));
                        if (current_tabindex == 2) {
                            $$('.infinite-scroll-preloader3').remove();
                            index_tab3--;
                        }
                    }
                    yy = "";
                },
                error: function (xhr, status) {
                     myApp.hidePreloader();
                    console.log("ajax error!");
                    loading=false;
                    yy = "";
                }
            });

        } //end if() 餐厅
        else if (current_tabindex == 3) { // 开始娱乐
            var v_search = [];
            var v_order = [];
            if (yy != "") {
                v_search.push({
                    SearchField: "EntertainmentKeyword",
                    ConditionType: 2,
                    AndOR: "and",
                    ListSearchValue: [{
                        ItemValue: yy
                    }]
                });
            }
            var opt = {
                OrderField: "Distance",
                OrderType: "asc"
            };
            v_order.push(opt);
            var _content = JSON.stringify({
                type: 0,
                conditionParams: {
                    Page: {
                        PageSize: pagesize,
                        PageIndex: pageindex
                    },
                    Search: v_search,
                    Order: v_order,
                },
                lng: iss_localStorage.get("yuqing_longitude"),
                lat: iss_localStorage.get("yuqing_latitude"),
            });

            $$.ajax({
                url: ISS_Constants.Url.defaulturl + ISS_Constants.serviceName.WEB_ENTERTAINMEN,
                data: { content: _content },
                crossDomain: true,
                dataType: "jsonp",
                success: function (data, status) {
                     myApp.hidePreloader();
                    data = JSON.parse(data);
                    loading=false;
                    if (data && data.status == "0" && data.results.length > 0) {
                        // 格式化数据
                        for (var i = 0; i < data.results.length; i++) {
                            if (data.results[i].getImage && data.results[i].getImage.smallImageFrontUrl && (
                                data.results[i].getImage.smallImageFrontUrl == null || data.results[i].getImage.smallImageFrontUrl == "")) {
                                data.results[i].getImage.smallImageFrontUrl = NoPicture();
                            };
                            data.results[i].distance = ISS.utils.toFixed(data.results[i].distance, 2);
                        }
                        var searchTemplate = $$('#template-nearby-tab4').html();
                        if (searchTemplate) {
                            var compiledTemplate = Template7.compile(searchTemplate);
                            var html = compiledTemplate(data);
                            $$("#sub_nearby_tab4 ul").append(html);
                            flag_InfiniteScroll = flag_needScroll = true;
                        }
                        if ($$("sub_nearby_tab4 .content-block ul li").length >= data.total) {
                            // 删除加载提示符
                            $$('.infinite-scroll-preloader4').remove();
                            // 加载完毕，则注销无限加载事件，以防不必要的加载
                            myApp.detachInfiniteScroll($$('.infinite-scroll'));
                        }
                    }
                    else {
                        // 删除加载提示符
                        myApp.detachInfiniteScroll($$('.infinite-scroll'));
                        if (current_tabindex == 3) {
                            $$('.infinite-scroll-preloader4').remove();
                            index_tab4--;
                        }
                    }
                    yy = "";
                },
                error: function (xhr, status) {
                     myApp.hidePreloader();
                    console.log("ajax error!");
                    loading=false;
                    yy = "";
                }
            });

        } //end if() 娱乐
        else if (current_tabindex == 4) { // 开始购物
            var v_search = [];
            var v_order = [];
            if (yy != "") {
                v_search.push({
                    SearchField: "cnName",
                    ConditionType: 2,
                    AndOR: "and",
                    ListSearchValue: [{
                        ItemValue: yy
                    }]
                });
            }
            var opt = {
                OrderField: "Distance",
                OrderType: "asc"
            };
            v_order.push(opt);
            var _content = JSON.stringify({
                type: 0,
                conditionParams: {
                    Page: {
                        PageSize: pagesize,
                        PageIndex: pageindex
                    },
                    Search: v_search,
                    Order: v_order,
                },
                lng: iss_localStorage.get("yuqing_longitude"),
                lat: iss_localStorage.get("yuqing_latitude"),
            });
            $$.ajax({
                url: ISS_Constants.Url.defaulturl + ISS_Constants.serviceName.BUSINESS,
                data: { content: _content },
                crossDomain: true,
                dataType: "jsonp",
                success: function (data, status) {
                     myApp.hidePreloader();
                    data = JSON.parse(data);
                    loading=false;
                    if (data && data.status == "0" && data.results.length > 0) {
                        // 格式化数据
                        for (var i = 0; i < data.results.length; i++) {
                            if (data.results[i].getImage && data.results[i].getImage.smallImageFrontUrl && (
                                data.results[i].getImage.smallImageFrontUrl == null || data.results[i].getImage.smallImageFrontUrl == "")) {
                                data.results[i].getImage.smallImageFrontUrl = NoPicture();
                            };
                            data.results[i].distance = ISS.utils.toFixed(data.results[i].distance, 2);
                        }
                        var searchTemplate = $$('#template-nearby-tab5').html();
                        if (searchTemplate) {
                            var compiledTemplate = Template7.compile(searchTemplate);
                            var html = compiledTemplate(data);
                            $$("#sub_nearby_tab5 ul").append(html);
                            flag_InfiniteScroll = flag_needScroll = true;
                        }
                        if ($$("sub_nearby_tab5 .content-block ul li").length >= data.total) {
                            // 删除加载提示符
                            $$('.infinite-scroll-preloader5').remove();
                            // 加载完毕，则注销无限加载事件，以防不必要的加载
                            myApp.detachInfiniteScroll($$('.infinite-scroll'));
                        }
                    }
                    else {
                        // 删除加载提示符
                        myApp.detachInfiniteScroll($$('.infinite-scroll'));
                        if (current_tabindex == 4) {
                            $$('.infinite-scroll-preloader5').remove();
                            index_tab5--;
                        }
                    }
                    yy = "";
                },
                error: function (xhr, status) {
                     myApp.hidePreloader();
                    console.log("ajax error!");
                    loading=false;
                    yy = "";
                }
            });

        } //end if() 购物
    };

    // 加载flag
    var loading = false;
    // 注册无限加载'infinite'事件处理函数
    $$('.infinite-scroll').on('infinite', function () {
        //综合 价格 距离 切换
        if (!flag_needScroll) {
            flag_needScroll = true;
            return;
        }

        //查询时,删除滚动后重新绑定pageindex
        if (!flag_InfiniteScroll) {
            flag_InfiniteScroll = true;
            return;
        }

        // 如果正在加载，则退出
        if (loading) return;

        // 设置flag
        loading = true;

        // 模拟1s的加载过程
        setTimeout(function () {
            if (current_tabindex == 0) {
                index_tab1++;
                LoadData(index_tab1, size);
            }
            else if (current_tabindex == 1) {
                index_tab2++;
                LoadData(index_tab2, size);
            }
            else if (current_tabindex == 2) {
                index_tab3++;
                LoadData(index_tab3, size);
            }
            else if (current_tabindex == 3) {
                index_tab4++;
                LoadData(index_tab4, size);
            }
            else if (current_tabindex == 4) {
                index_tab5++;
                LoadData(index_tab5, size);
            };
        }, 800);
    });

    // 切换
    $$(".subnavbar").on("click", "a", function () {
        $$('.infinite-scroll').scrollTop(0);
        current_tabindex = $$(this).index();
        myApp.attachInfiniteScroll($$('.infinite-scroll'));
        $$(".noAnswer").remove();

        flag_InfiniteScroll = true;
        flag_needScroll = false;
        if ($$(this).index() == 0 && $$("#sub_nearby_tab1 .content-block ul").children("li").length == 0) {
            LoadData(index_tab1, size);
        }
        else if ($$(this).index() == 1 && $$("#sub_nearby_tab2 .content-block ul").children("li").length == 0) {
            LoadData(index_tab2, size);
        }
        else if ($$(this).index() == 2 && $$("#sub_nearby_tab3 .content-block ul").children("li").length == 0) {
            LoadData(index_tab3, size);
        }
        else if ($$(this).index() == 3 && $$("#sub_nearby_tab4 .content-block ul").children("li").length == 0) {
            LoadData(index_tab4, size);
        }
        else if ($$(this).index() == 4 && $$("#sub_nearby_tab5 .content-block ul").children("li").length == 0) {
            LoadData(index_tab5, size);
        }
    });
});

/* 初始化  */
function initData() {
    var href = window.location.href;
    if (href.indexOf(nearby) > -1) {
        contactsCallback_nearby.trigger();
    }
}

initData()
// ISS.utils.initLocation(initData)



